package com.syg.boot.util.net;

/**
 * @author syg
 * @date 2021年11月24日19:43:12
 * @version 1.0
 */
public class OsUtil {
    public static final OsUtil.OsType OS_TYPE = getOsType();
    public static final boolean WINDOWS;
    public static final boolean SOLARIS;
    public static final boolean MAC;
    public static final boolean FREEBSD;
    public static final boolean LINUX;
    public static final boolean OTHER;

    public OsUtil() {
    }

    public static boolean isWin() {
        OsUtil.OsType osType = getOsType();
        return OsUtil.OsType.OS_TYPE_WIN.equals(osType);
    }

    public static boolean isLinux() {
        OsUtil.OsType osType = getOsType();
        return OsUtil.OsType.OS_TYPE_LINUX.equals(osType);
    }

    public static boolean isMac() {
        OsUtil.OsType osType = getOsType();
        return OsUtil.OsType.OS_TYPE_MAC.equals(osType);
    }

    public static OsUtil.OsType getOsType() {
        String osName = System.getProperty("os.name");
        if (osName.startsWith("Windows")) {
            return OsUtil.OsType.OS_TYPE_WIN;
        } else if (!osName.contains("SunOS") && !osName.contains("Solaris")) {
            if (osName.contains("Mac")) {
                return OsUtil.OsType.OS_TYPE_MAC;
            } else if (osName.contains("FreeBSD")) {
                return OsUtil.OsType.OS_TYPE_FREEBSD;
            } else {
                return osName.startsWith("Linux") ? OsUtil.OsType.OS_TYPE_LINUX : OsUtil.OsType.OS_TYPE_OTHER;
            }
        } else {
            return OsUtil.OsType.OS_TYPE_SOLARIS;
        }
    }

    static {
        WINDOWS = OS_TYPE == OsUtil.OsType.OS_TYPE_WIN;
        SOLARIS = OS_TYPE == OsUtil.OsType.OS_TYPE_SOLARIS;
        MAC = OS_TYPE == OsUtil.OsType.OS_TYPE_MAC;
        FREEBSD = OS_TYPE == OsUtil.OsType.OS_TYPE_FREEBSD;
        LINUX = OS_TYPE == OsUtil.OsType.OS_TYPE_LINUX;
        OTHER = OS_TYPE == OsUtil.OsType.OS_TYPE_OTHER;
    }

    /**
     * 系统类型
     */
    public static enum OsType {
        // linux
        OS_TYPE_LINUX,
        // window
        OS_TYPE_WIN,
        // SOLARIS
        OS_TYPE_SOLARIS,
        // MAX
        OS_TYPE_MAC,
        // FREEBSD
        OS_TYPE_FREEBSD,
        // OTHER
        OS_TYPE_OTHER;

        private OsType() {
        }
    }
}
